package com.zzyang.mybatis.jdbc.spring.config;

import com.zzyang.mybatis.jdbc.core.*;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 自动配置
 *
 * @author zhuoziyang
 * @date 2021/4/8 23:05
 */
@Configuration
@ConditionalOnClass(SqlSessionTemplate.class)
@EnableConfigurationProperties(SeparationJdbcProperties.class)
public class SeparationJdbcAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public DataSourceContext dataSourceContext(SeparationJdbcProperties properties) {
        return new DataSourceContext(properties);
    }

    @Bean
    @Primary
    public DefaultRoutingDataSource defaultRoutingDataSource() {
        return new DefaultRoutingDataSource();
    }

    @Bean
    @ConditionalOnMissingBean
    public RoutingDataSourceKeyDecider routingDataSourceKeyDecider() {
        return new DefaultRoutingDataSourceKeyDecider();
    }

    @Bean
    public ExecutorInterceptor executorInterceptor() {
        return new ExecutorInterceptor();
    }

}
